package com.handinfo.android.uicontrols.controls;

import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWAbsHyperlink;
import com.handinfo.android.uicontrols.DWControlTools;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWHyperlink;
import com.handinfo.android.utils.Tools;

/**
 * 文本框
 * @author handinfo
 */
public class DWTextbox extends DWControl
{

	private String[] m_contents = null;
	private DWAbsHyperlink m_hyperlink = null;
	private DWScrollBar m_scrollbar = null;
	private float m_offset_x = 0;
	private float m_offset_y = 0;
	private DWFont m_font = DWFont.getDefaultFont();
//	public static int LINESPACING = (int) (DWFont.getDefaultFont().getFontHeight() + 2);//单行高度
	private final int STOP_TIME = 10;
	private int m_stop_time = STOP_TIME;//滚动停留时间
	private int m_wait_time = 0;//滚动等待时间
	private char m_hyperlink_type = 0;
	private String m_hyperlink_key = null;
	private String m_hyperlink_content = null;
	private String m_text = null;
	private boolean m_change_row = false;
	

	private static final byte TEXT_TYPE_RECT = 0;//显示在一个矩形内,带换行和滚动
	private static final byte TEXT_TYPE_WIDTH = 1;//限定一个宽度内,带换行,无滚动,全部显示
	private static final byte TEXT_TYPE_SINGLE = 2;//单行显示
	private byte m_text_type = TEXT_TYPE_RECT;//是否需要换行的

	/**
	 * 创建一个文本框,显示在指定的w,h内.
	 * @param text 文本
	 * @param w 宽度w
	 * @param h 高度h
	 */
	public DWTextbox(String text, int w, int h)
	{
		this(text, 0, 0, w, h);
	}
	
	/**
	 * 创建一个文本框,显示在指定的w,h内.
	 * @param text
	 * @param w
	 * @param h
	 * @param line 是否不支持`换行
	 */
	public DWTextbox(String text, int w, int h, boolean line) {
		m_change_row = line;
		m_text_type = TEXT_TYPE_RECT;
		setShowWideHigh(w, h);
		setText(text);
	}

	/**
	 * 创建一个文本框,显示在指定的x,y以及w,h内.
	 * @param text 文本
	 * @param x 坐标x
	 * @param y 坐标y
	 * @param w 宽度w
	 * @param h 高度h
	 */
	public DWTextbox(String text, int x, int y, int w, int h)
	{
		m_text_type = TEXT_TYPE_RECT;
		setPoint(x, y);
		setShowWideHigh(w, h);
		setText(text);
	}

	/**
	 * 创建一个文本框,带换行,全部显示
	 * @param text
	 * @param w
	 */
	public DWTextbox(String text, int w)
	{
		this(text, 0, 0, w);
	}

	/**
	 * 创建一个文本框,带换行,全部显示
	 * @param text
	 * @param x
	 * @param y
	 * @param w
	 */
	public DWTextbox(String text, int x, int y, int w)
	{
		m_text_type = TEXT_TYPE_WIDTH;
		setPoint(x, y);
		setShowWideHigh(w, 500);
		setText(text);
	}

	/**
	 * 创建一个文本框
	 * @param text 文本
	 */
	public DWTextbox(String text)
	{
		m_text_type = TEXT_TYPE_SINGLE;
		setText(text);
	}

	/**
	 * 设置一个文本
	 * @param text
	 */
	public void setText(String text)
	{
		m_text = text;
		m_hyperlink = null;
		m_contents = null;
		if (text == null) {
			return;
		}
		switch (m_text_type) {
			case TEXT_TYPE_RECT:
				if (DWAbsHyperlink.isHyperLink(text)) {
					m_hyperlink = DWAbsHyperlink.createHyperLinker(text, m_show_w, m_show_h, m_font, m_change_row);
					m_actual_h = m_hyperlink.getRealHeight();
					if (m_actual_h < (m_font.getHeight() + 2)) {
						m_actual_h = (m_font.getHeight() + 2);
					}
				} else {
					m_contents = DWControlTools.splitString(text, m_font, DWControlTools.CHANGE_ROW, m_show_w, 0, m_change_row);
					m_actual_h = m_contents.length * (m_font.getHeight() + 2);
				}
				if (m_actual_h > m_show_h) {
					m_scrollbar = new DWScrollBar(m_show_h);
					m_scrollbar.setNearAnchor(this, Tools.LEFT_TOP, Tools.RIGHT_TOP, 2, 0);
				} else {
					if (m_scrollbar != null) {
						m_scrollbar.destroy();
						m_scrollbar = null;
					}
				}
				break;
			case TEXT_TYPE_WIDTH:
				if (DWAbsHyperlink.isHyperLink(text)) {
					m_hyperlink = DWAbsHyperlink.createHyperLinker(text, m_show_w, m_show_h, m_font, m_change_row);
					m_show_h = m_hyperlink.getRealHeight();
					if (m_show_h < (m_font.getHeight() + 2)) {
						m_show_h = (m_font.getHeight() + 2);
					}
				} else {
					m_contents = DWControlTools.splitString(text, m_font, DWControlTools.CHANGE_ROW, m_show_w, 0, m_change_row);
					m_show_h = m_contents.length * (m_font.getHeight() + 2);
				}
				setActualWideHigh(m_show_w, m_show_h);
				break;
			case TEXT_TYPE_SINGLE:
				if (DWAbsHyperlink.isHyperLink(text)) {
					m_hyperlink = DWAbsHyperlink.createHyperLinker(text, (int)m_font.stringWidth(text), (m_font.getHeight() + 2), m_font, m_change_row);
					m_actual_w = m_hyperlink.getRealWidth();
					m_actual_h = m_hyperlink.getRealHeight();
				} else {
					m_actual_w = (int)m_font.stringWidth(text);
					m_contents = DWControlTools.splitString(text, m_font, DWControlTools.CHANGE_ROW, m_actual_w, 0, m_change_row);
					m_actual_h = m_contents.length * (m_font.getHeight() + 2);
				}
				setShowWideHigh(m_actual_w, m_actual_h);
				break;
		}
	}
	
	/**
	 * 设置font
	 * @param font
	 */
	public void setFont(DWFont font) {
		m_font = font;
		setText(m_text);
	}

	/**
	 * 绘制
	 * @param g
	 */
	public void render(DWGraphics g)
	{
		if (m_hyperlink == null && m_contents == null) {
			return;
		}
		if (m_is_show) {
			g.setClip(m_show_x, m_show_y, m_show_w + 1, m_show_h + 1);
			if (m_hyperlink == null) {
				drawContents(g, (int) (m_show_x - m_offset_x), (int) (m_show_y - m_offset_y + 2));
			}
			else {
				m_hyperlink.paintRect(g, (int) (m_show_x - m_offset_x), (int) (m_show_y - m_offset_y + 2));
			}
			renderRect(g);
			if (m_scrollbar != null) {
				m_scrollbar.setDefaultNearAnchorUn();
				m_scrollbar.render(g);
			}
		}
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		if (m_hyperlink == null && m_contents == null) {
			return;
		}
		if (m_is_show) {
			setSubClip(g, x, y, w, h, offsetx, offsety);
			if (m_hyperlink == null) {
				drawContents(g, (int) (x + m_show_x - m_offset_x - offsetx), (int) (y + m_show_y - m_offset_y - offsety + 2));
			}
			else {
				m_hyperlink.paintRect(g, (int) (x + m_show_x - m_offset_x - offsetx), (int) (y + m_show_y - m_offset_y - offsety + 2));
			}
			renderLimitsRect(g, x, y, offsetx, offsety);
			if (m_scrollbar != null) {
				m_scrollbar.setDefaultNearAnchorUn();
				m_scrollbar.renderLimits(g, x, y, w, h, offsetx, offsety);
			}
		}
	}

	public void logic()
	{
		if (m_actual_w > m_show_w) {
			if (m_stop_time > 0) {
				m_stop_time--;
			}
			else if (m_wait_time > 0) {
				m_wait_time--;
				if (m_wait_time == 0) {
					m_offset_x = 0;
					m_stop_time = STOP_TIME;
				}
			}
			else {
				m_offset_x += 2;
				if (m_offset_x + m_show_w >= m_actual_w) {
					m_wait_time = STOP_TIME;
				}
			}
		}
	}

	public void refreshControl()
	{
	}
	
	/**
	 * 得到文本
	 * @return
	 */
	public String getText() {
//		if (m_text != null && DWAbsHyperlink.isHyperLink(m_text)) {
//			return m_hyperlink.m_content;
//		}
		return m_text;
	}

	/**
	 * 得到超链接类型
	 * @return
	 */
	public char getHyperlinkType() {
		return m_hyperlink_type;
	}
	
	/**
	 * 得到超链接的key
	 * @return
	 */
	public String getHyperlinkKey() {
		return m_hyperlink_key;
	}
	
	/**
	 * 得到超链接内容
	 * @return
	 */
	public String getHyperlnkContent() {
		return m_hyperlink_content;
	}
	
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			m_hyperlink_key = null;
			m_hyperlink_type = 0;
			if (m_hyperlink != null && m_hyperlink.getHyperLinkSize() > 0) {
				DWHyperlink hl = (DWHyperlink)m_hyperlink;
				char[] links = hl.m_markTypes;
				for (int i = 0; i < links.length; i++) {
					if (hl.isHyper(i)) {
						String[] contents = hl.m_contents[i];
						String content = null;
						for (int j = 0; j < contents.length; j++) {
							content = contents[j];
							float px = x - m_show_x;
							float py = y - m_show_y + m_offset_y;
							float rx = 0;
							if (j == 0) {// 起始位置
								rx = hl.m_xs[i];
							}
							float rw = rx + m_font.stringWidth(content);
							float ry = hl.m_ys[i] + j * (m_font.getHeight() + 2);
							float rh = hl.m_ys[i] + (j + 1) * (m_font.getHeight() + 2);
							if (px > rx && px < rw && py > ry && py < rh) {
								m_hyperlink_key = hl.m_ids[i];
								m_hyperlink_type = hl.m_markTypes[i];
								m_hyperlink_content = hl.m_contents_copy[i];
//								DWGameManager.getInstance().showToast("超链接类型 : " + hl.m_markTypes[i] + ",key : " + hl.m_ids[i], 10000);
								if (m_listener != null) {
									m_listener.OnClick();
								}
								return true;
							}
						}
					}
				}
			}
			if (m_listener != null) {
				m_listener.OnClick();
			}
			return true;
		}
		return false;
	}

	public boolean doDoubleClick(float pointX, float pointY)
	{
		return false;
	}

	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (checkPointOnRect(start.x, start.y) && m_actual_h > m_show_h) {
			float b = m_actual_h / m_show_h;
			m_offset_y += (distanceY / b) * 2;
			if (m_offset_y < 0) {
				m_offset_y = 0;
			}
			else if (m_offset_y + m_show_h > m_actual_h) {
				m_offset_y = m_actual_h - m_show_h;
			}
			m_offset_x = 0;
			m_stop_time = STOP_TIME;

			if (m_scrollbar != null) {
				m_scrollbar.setMark((int) (m_offset_y * 100 / (m_actual_h - m_show_h)));
			}
			return true;
		}
		return false;
	}

	private void drawContents(DWGraphics g, int x, int y)
	{
		String[] texts = m_contents;
		if (texts != null) {
			for (int i = 0; i < texts.length; i++) {
				g.drawShadowString(m_font, texts[i], DWControlsManager.COLOR_STROKE, Tools.WHITE, x, y + i * (m_font.getHeight() + 2), Tools.LEFT_TOP);
			}
		}
	}

}
